//
//  UnarmedSearchView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/3.
//

import Foundation
import UIKit

class UnarmedSearchView: UIView {
    
    let netItem = UnarmedSearchItem(withPlaceholder: "网点名称：")
    let typeItem = UnarmedSearchItem(withPlaceholder: "布防类型：")
    let dateItem = UnarmedSearchItem(withPlaceholder: "布防日期：")
    
    let submitBtn = UIButton.createPrimaryLarge("查询")

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(netItem)
        netItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top).offset(10)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(typeItem)
        typeItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(netItem.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(dateItem)
        dateItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(typeItem.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(dateItem.snp.bottom).offset(31)
            make.height.equalTo(50)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
    }
}

class UnarmedSearchItem: UIControl {
    var value: String? {
        set {
            valueL.text = newValue
            updatValue(newValue)
        }
        get {
            return valueL.text
        }
    }
    
    var placeholder: String = "请选择" {
        didSet {
            updatePlaceholder()
        }
    }
    
    var dataSource: [LabelDictModal] = []

    let keyL = UILabel()
    let valueL = UILabel()
    let arrowIV = UIImageView(image: UIImage(systemName: "chevron.right"))
    
    init(withPlaceholder _place: String) {
        super.init(frame: .zero)
        self.placeholder = _place
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updatePlaceholder() {
        keyL.text = placeholder
    }
    
    func updatValue(_ v: String? = nil) {
        if v == nil || v == "" {
            valueL.textColor = .hex("#C4C4C4")
            valueL.text = "请选择"
        } else {
            valueL.textColor = .black
            valueL.text = v
        }
    }
    
    func setupUI() {
        backgroundColor = .white
        
        keyL.text = placeholder
        keyL.textColor = .hex("#666666")
        keyL.font = UIFont.systemFont(ofSize: 16)
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.centerY.equalTo(self.snp.centerY)
            make.left.equalTo(self.snp.left).offset(11)
        }
        
        arrowIV.tintColor = .hex("#C4C4C4")
        arrowIV.contentMode = .scaleAspectFit
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp.centerY)
            make.right.equalTo(self.snp.right).offset(-15)
            make.width.equalTo(14)
        }
        
        valueL.font = UIFont.systemFont(ofSize: 16)
        addSubview(valueL)
        valueL.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp.centerY)
            make.right.equalTo(arrowIV.snp.left).offset(-10)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#F3F3F3")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(0.5)
        }
        
        updatValue()
    }
    
    override var isHighlighted: Bool {
        didSet {
            if isHighlighted {
                backgroundColor = .hex("#ECECEC")
            } else {
                backgroundColor = .white
            }
        }
    }
}
